package cn.pet.service.manage.controller;


import cn.pet.service.manage.common.Response;
import cn.pet.service.manage.entity.PetCart;
import cn.pet.service.manage.entity.PetGoods;
import cn.pet.service.manage.entity.api.Result;
import cn.pet.service.manage.entity.api.StatusCode;
import cn.pet.service.manage.service.PetCartService;
import cn.pet.service.manage.service.PetGoodsService;
import cn.pet.service.manage.utils.SecurityUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author 
 * @since 2023-02-11
 */
@RestController
@RequestMapping("/manage/petCart")
@Slf4j
public class PetCartController {

    @Resource
    private PetCartService petCartService;
    @Resource
    private PetGoodsService goodsService;

    /**
     * 加入购物车
     * @param petCart
     * @return
     */
    @PostMapping("add")
    public Object add(@RequestBody PetCart petCart) {
        Integer userId = SecurityUtil.getUserId();
        if (userId == null){
            return Result.error("请先登录");
        }
        petCart.setUserId(userId);
        LambdaQueryWrapper<PetCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PetCart::getGoodId,petCart.getGoodId())
                .eq(PetCart::getUserId,petCart.getUserId());
        PetCart one = petCartService.getOne(queryWrapper);
        //获取商品的价格
        PetGoods petGoods = goodsService.getById(petCart.getGoodId());
        if (one == null){
            if (petCart.getNum() == null) {
                petCart.setNum(1);
            }
            petCart.setMoney(petCart.getNum()*petGoods.getPrice());
            if (!petCartService.save(petCart)){
                return Result.error("添加失败");
            }
            return Result.ok("添加成功");
        }
        if (petCart.getNum() == null) {
            petCart.setNum(1);
        }
        //存在该购物车则新增购物车信息
        petCart.setId(one.getId());
        petCart.setNum(petCart.getNum()+one.getNum());
        petCart.setMoney(petCart.getNum()*petGoods.getPrice());
        if (!petCartService.updateById(petCart)){
            return Result.error("添加失败");
        }
        return Result.ok("添加成功");
    }

    @PostMapping("edit")
    public Object edit(@RequestBody PetCart petCart){
        if (petCart.getNum()!=null && petCart.getNum()==0){
            //删除
            petCartService.removeById(petCart.getId());
            return Result.ok("修改成功");
        }
        petCart.setMoney(petCart.getNum()*petCart.getPrice());
        petCartService.updateById(petCart);
        return Result.ok("修改成功");
    }

    /**
     * 根据用户ID查询购物车列表
     * @param
     * @return
     */
    @GetMapping("listByUserId")
    public Object listByUserId(){
        Integer userId = SecurityUtil.getUserId();
        return Result.ok("success",petCartService.getListByUserId(userId));
    }



    /**
     * 删除购物车中商品
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") Integer id){
        if (!petCartService.removeById(id)){
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }

    /**
     * 清空购物车
     * @param
     * @return
     */
    @DeleteMapping("deleteAll")
    public Object deleteAll(){
        Integer userId = SecurityUtil.getUserId();
        //根据用户ID查询购物车所有商品
        LambdaQueryWrapper<PetCart> petCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        petCartLambdaQueryWrapper.eq(PetCart::getUserId,userId);
        List<Integer> carIds = petCartService.list(petCartLambdaQueryWrapper).stream().map(PetCart::getId).collect(Collectors.toList());
        if (!petCartService.removeBatchByIds(carIds)){
            return Result.error("清空失败");
        }
        return Result.ok("清空成功");
    }

    /**
     * 获取用户购物车的数量
     * @return
     */
    @GetMapping("cartCount")
    public Object cartCount(){
        LambdaQueryWrapper<PetCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PetCart::getUserId,SecurityUtil.getUserId());
        return Result.ok("获取成功",petCartService.count(queryWrapper));
    }





}
